package com.tian.manager.realm;

import com.tian.manager.entity.SysManager;
import com.tian.manager.service.SysManagerService;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

public class CustomRealm extends AuthorizingRealm {


    @Override
    public void setName(String name) {
        super.setName("CustomRealm");
    }

    @Autowired
    private SysManagerService managerService;

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        UsernamePasswordToken usernamePasswordToken = (UsernamePasswordToken) authenticationToken;
        String username = usernamePasswordToken.getUsername();
        String password = new String(usernamePasswordToken.getPassword());
        SysManager manager = managerService.findByName(username);
        if(manager!=null&& manager.getManagerPwd().equals(password)){
            return new SimpleAuthenticationInfo(manager,manager.getManagerPwd(),this.getName());
        }else{
            return  null;
        }
    }
}
